#include <stdio.h>
#include <mpx_supt.h>


date_rec* dateP = new date_rec;
void help(char* arg)
{
	int arglen = strlen(arg);
	//help_list should be moved and made const global in header
	static char help_list[7][800];
	strcpy(help_list[0],"\t date [-s mm/dd/yyyy] \t Displays current date.\t-s argument used to set date.\n");
	strcpy(help_list[1],"\t cls \t\t Clears screen.\n");
	strcpy(help_list[2],"\t ls [path] \t Displays contents of directory specified. Displays \".\" by default.\n");
	strcpy(help_list[3],"\t help [command] \t\t Displays help menu for [command] or whole menu if blank.\n");
	strcpy(help_list[4],"\t vrsn \t\t Displays version of MPX.\n");
	strcpy(help_list[5],"\t exit \t\t Used to exit the program.\n");
	strcpy(help_list[6],"\t pcb [-new],[-del],[-blok],[-unblok],[-paus],[-play],[-setp],[-meta] use pcb and its arguments to manipulate process blocks.\n");
	if(arglen != 0){
		printf("\n");
		if(!strcmp(arg, "date")){
			printf("%s", help_list[0]);}
		else if(!strcmp(arg, "cls")){
			printf("%s", help_list[1]);}
		else if(!strcmp(arg, "ls")){
			printf("%s", help_list[2]);}
		else if(!strcmp(arg, "help")){
			printf("%s", help_list[3]);}
		else if(!strcmp(arg, "vrsn")){
			printf("%s", help_list[4]);}
		else if(!strcmp(arg, "exit")){
			printf("%s", help_list[5]);}
		else if(!strcmp(arg, "pcb")){
			printf("%s", help_list[6]);}
		else 
			printf("\"%s\" is not a recognized command.\n", arg);
	}
	else{
		printf("\nThis is a comprehensive list of commands and how they are used.\nCOMMAND: \t\t DESCRIPTION:\n");
		for(int i=0; i<6; i++){
			printf("%s\n", help_list[i]);
		}
	}
}

//make version contant global in header
void display_version()
{
	printf("\nVersion 1.6\n\n");
}

void display_date()
{
	sys_get_date(dateP);
	printf("\n%d/", dateP->month);
	printf("%d/", dateP->day);
	printf("%d\n\n", dateP->year);
}

void set_date(int m, int d, int y)
{
	printf("m:%d d:%d y:%d\n", m,d,y);
	int valid = 1;           //date validation parameter

	if(y<0) {valid=0;}    //Year is a negative number

	if(m>12 || m<1) {valid=0;} //Month must be 1-12

	if(m==2 && d>28) {valid=0;}  //28 days in feb

	else if((m==4 || m==6 || m==9 || m==11) && d>30) {valid=0;}//30 days

	else if((m==1 || m==3 || m==5 || m==7 || m==8
	|| m==10 || m==12) && d>31) {valid=0;}        //31 day months


	if(valid)           //Valid date, execute sys_set_date()
	{
		dateP->month = m;
		dateP->day = d;
		dateP->year = y;
		sys_set_date(dateP);
	}

	else if(valid==0)
	{ printf("Invalid date, please try again.\n");}
}

void date_cmd(char* date_str){
	const int arglen = strlen(date_str);
	if((date_str[0]=='-')&&(tolower(date_str[1])=='s')){
		int n = 2;
		int p = 0;
		char cdate[10];
		while(n<arglen){
			if((date_str[n] != ' ')&&(date_str[n] != '\t')){
				cdate[p]=date_str[n];
				p++;
			}
			n++;
		}
		int m = ((int(cdate[0])-48)*10)+(int(cdate[1])-48);
		int d = ((int(cdate[3])-48)*10)+(int(cdate[4])-48);
		int y = (((int(cdate[6])-48)*1000)+((int(cdate[7])-48)*100)+((int(cdate[8])-48)*10)+(int(cdate[9])-48));
		set_date(m, d, y);
	}
	else{
		printf("\"%s\" is not a recognizable argument.\n", date_str);
	}
}

void terminateMPX()
{
	sys_exit();
}

void clear_screen(){
	int bs = 1;
	char buf[1];
	sys_req(CLEAR, TERMINAL, buf, &bs);
}
//removes all whitespace from a string
char* gutspaces(char* str){
	int strL = strlen(str);
	char str_new[strL]={'\0'};
	for(int x=0; x<strL; x++){
		if(!((str[x]=' ')||(str[x]=='\t')||(str[x]=='\n'))){
			str_new[x] = str[x];
		}}
	return str_new;
}